import request from '../utils/request';
import role from '../route/modules/role';
import user from '../store/modules/user';

// /**
//  * 用户登录接口
//  * @param {string} identity 用户唯一标识（用户名、邮箱或手机号）
//  * @param {string} password 用户密码（MD5加密）
//  */
// export function loginApi(identity, password) {
//   return request({
//     url: '/users/login',
//     method: 'post',
//     data: {
//       identifier: identity,
//       password: password
//     }
//   });
// }

/**
 * 用户登录接口
 * @param {string} identity 用户唯一标识（用户名、邮箱或手机号）
 * @param {string} password 用户密码（MD5加密）
 */
export function loginApi(identity, password, geetestChallenge, geetestValidate, geetestSeccode) {
  return request({
    url: '/users/validate',
    method: 'post',
    data: {
      identifier: identity,
      password: password,
      geetest_challenge: geetestChallenge,
      geetest_validate: geetestValidate,
      geetest_seccode: geetestSeccode
    }
  });
}

/**
 * 创建新用户
 * @param {string} username 账户
 * @param {string} displayName 用户名
 * @param {string} password 密码
 * @param {number} jobId 岗位id
 * @param {number[]} organizationIdList 组织id列表
 * @param {string[]} roleUuidList 角色uuid列表
 * @param {string} userType 用户类型
 * @param {boolean} active
 * @param {string} email 邮箱
 * @param {string} mobile 手机号
 */
export function createUserApi(username, displayName, password, jobId, organizationIdList, roleUuidList, active, email, mobile) {
  return request({
    url: '/users',
    method: 'post',
    data: {
      username,
      display_name: displayName || null,
      password,
      job_id: jobId,
      organization_id_list: organizationIdList,
      role_uuid_list: roleUuidList,
      active,
      email: email || null,
      mobile: mobile || null
    }
  });
}

/**
 * 删除用户
 * @param {string} uuid 用户uuid
 */
export function deleteUserApi(uuid) {
  return request({
    url: '/users/' + uuid,
    method: 'delete'
  });
}

/**
 * 修改新用户
 * @param {string} uuid 用户uuid
 * @param {string} username 账户
 * @param {string} displayName 用户名
 * @param {string} password 密码
 * @param {number} jobId 岗位id
 * @param {number[]} organizationIdList 组织id列表
 * @param {string[]} roleUuidList 角色uuid列表
 * @param {string} userType 用户类型
 * @param {boolean} active 是否可用
 * @param {string} email 邮箱
 * @param {string} mobile 手机号
 */
export function updateUserApi(uuid, username, displayName, jobId, organizationIdList, roleUuidList, active, email, mobile) {
  return request({
    url: '/users/' + uuid,
    method: 'put',
    data: {
      username,
      display_name: displayName,
      job_id: jobId,
      organization_id_list: organizationIdList,
      role_uuid_list: roleUuidList,
      active,
      email: email || null,
      mobile: mobile || null
    }
  });
}

/**
 * 用户更改密码
 * @param {string} oldPassword 旧密码(md5加密)
 * @param {string} newPassword 新密码(md5加密)
 */
export function updateUserPasswordApi(oldPassword, newPassword) {
  return request({
    url: '/users/password/change',
    method: 'patch',
    data: {
      old_password: oldPassword,
      new_password: newPassword
    }
  });
}

/**
 * 用户更改密码
 * @param {string} password 新密码(md5加密)
 * @param {string} userUuid 用户uuid
 */
export function resetUserPasswordApi(userUuid, password) {
  return request({
    url: '/users/password/reset',
    method: 'patch',
    params: {
      user_uuid: userUuid,
      password: password
    }
  });
}

/**
 * 获取用户列表
 * @param {number} pageIndex 分页页码
 * @param {number} pageSize 分页每页数量
 * @param {string} sortType 排列顺序
 * @param {string} orderBy 排序字段
 * @param {string} name 用户名
 * @param {string} organizationId 组织id
 * @param {string} email 邮箱
 * @param {number} mobile 手机
 */
export function getUserListApi(pageIndex, pageSize, sortType, orderBy, organizationId, name, email, mobile, roleUuid, includeRole) {
  return request({
    url: '/users',
    method: 'get',
    params: {
      page_index: pageIndex,
      page_size: pageSize,
      sort_type: sortType,
      order_by: orderBy,
      organization_id: organizationId || null,
      name: name || null,
      email: email || null,
      mobile: mobile || null,
      role_uuid: roleUuid || null,
      include_role: includeRole || null
    }
  });
}

/**
 * 根据角色uuid获取用户列表
 * @param {number} pageIndex 分页页码
 * @param {number} pageSize 分页每页数量
 * @param {string} sortType 排列顺序
 * @param {string} uuid 角色uuid
 */
export function getUserListByRoleUuidApi(pageIndex, pageSize, sortType, uuid) {
  return request({
    url: '/users/role',
    method: 'get',
    params: {
      page_index: pageIndex,
      page_size: pageSize,
      sort_type: sortType,
      uuid
    }
  });
}

/**
 * 批量更新用户角色
 * @param {string} roleUuid 角色uuid
 * @param {string[]} userUuidList 用户uuid列表
 */
export function updateUserRoleBatchApi(roleUuid, userUuidList) {
  return request({
    url: '/users/roles/' + roleUuid,
    method: 'patch',
    data: {
      user_uuid_list: userUuidList
    }
  });
}

// 获取用户岗位列表
export function getUserJobsApi() {
  return request({
    url: '/users/jobs',
    method: 'get'
  });
}

/**
 * 切换岗位
 * @param {number} id 岗位id
 */
export function updateUserJobApi(id) {
  return request({
    url: 'users/jobs/' + id,
    method: 'patch'
  });
}
